package com.dongzhu1428.express.module1.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dongzhu1428.express.module1.Result;
import com.dongzhu1428.express.module1.dto.AddAddressParamDto;
import com.dongzhu1428.express.module1.entity.DeliveryAddress;
import com.dongzhu1428.express.module1.mapper.express.DeliveryAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址模块
 */
@RestController
@RequestMapping("/address")
public class AddressController {
	@Autowired
	DeliveryAddressMapper deliveryAddressMapper;

	/**
	 * 添加地址
	 * @param dto
	 */
	@PostMapping("/addAddress")
	Result<String> addAddress(@RequestBody AddAddressParamDto dto){
		deliveryAddressMapper.insert(DeliveryAddress.builder()
						.userId(dto.getUserId())
						.name(dto.getName())
						.phone(dto.getPhone())
						.address(dto.getAddress())
						.detailAddress(dto.getDetailAddress())
				.build());
		return  Result.success("添加地址成功");
	}

	/**
	 * 修改地址
	 * @param dto
	 */
	@PostMapping("/setAddress")
	Result<String> setAddress(@RequestBody AddAddressParamDto dto){
		deliveryAddressMapper.updateById(DeliveryAddress.builder()
						.id(dto.getId())
				.userId(dto.getUserId())
				.name(dto.getName())
				.phone(dto.getPhone())
				.address(dto.getAddress())
				.detailAddress(dto.getDetailAddress())
				.build());
		return  Result.success("修改成功");
	}

	/**
	 * 删除地址
	 * @param id
	 */
	@GetMapping("/deleteAddress")
	Result<String> addAddress(@RequestParam int id){
		deliveryAddressMapper.deleteById(id);
		return Result.success("删除成功");
	}

	/**
	 * 获取指定用户的地址列表
	 * @param userId
	 * @return
	 */
	@GetMapping("/getAddressList")
	Result<List<DeliveryAddress>> getAddressList(@RequestParam int userId){
		List<DeliveryAddress> addressList = deliveryAddressMapper.selectList(new QueryWrapper<DeliveryAddress>().eq("user_id", userId));
		return  Result.success(addressList);
	}

}
